package com.starry.home.utils

import android.annotation.SuppressLint
import android.os.Build
import androidx.annotation.RequiresApi
import com.starry.home.logic.model.hospital.DateBean
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.Date

/**
 * 日期转换工具类
 */
object DateUtils {

    val yyyyMMdd: ThreadLocal<DateFormat?> = object : ThreadLocal<DateFormat?>() {
        @SuppressLint("SimpleDateFormat")
        override fun initialValue(): DateFormat {
            return SimpleDateFormat("yyyy-MM-dd")
        }
    }

    /**
     * yyyyMMdd
     */
    val yyyyMMddNoSep: ThreadLocal<DateFormat?> = object : ThreadLocal<DateFormat?>() {
        @SuppressLint("SimpleDateFormat")
        override fun initialValue(): DateFormat {
            return SimpleDateFormat("yyyyMMdd")
        }
    }

    val yyyyMMddHHmmSS: ThreadLocal<DateFormat?> = object : ThreadLocal<DateFormat?>() {
        @SuppressLint("SimpleDateFormat")
        override fun initialValue(): DateFormat? {
            return SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        }
    }


    @RequiresApi(Build.VERSION_CODES.O)
    fun dateWeek(offset: Long): DateBean {
        val today = LocalDate.now().plusDays(offset)
        val dayOfWeekToday = today.dayOfWeek
        val week = when (dayOfWeekToday.toString()) {
            "FRIDAY" -> "周五"
            "SATURDAY" -> "周六"
            "SUNDAY" -> "周日"
            "MONDAY" -> "周二"
            "WEDNESDAY" -> "周三"
            else -> "周一"
        }
        val day = today.format(DateTimeFormatter.ofPattern("MM-dd"))
        val dayString = today.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
        return DateBean(day, week, dayString)
    }

    /**
     * 将 Date 类型转为时间字符串
     *
     * 格式为 format
     *
     * @param date   Date 类型时间
     * @param format 时间格式
     * @return 时间字符串
     */
    fun date2String(date: Date, format: DateFormat?): String {
        return if (format != null) {
            format.format(date)
        } else {
            ""
        }
    }
}